package com.filldream.fastboot.core.manager;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.filldream.fastboot.base.entity.Account;
import com.filldream.fastboot.base.mapper.AccountMapper;
import com.filldream.fastboot.base.response.app.AccountInfoDTO;
import com.filldream.fastboot.base.response.app.AccountManagerDTO;
import com.filldream.fastboot.common.entity.ErrorCode;
import com.filldream.fastboot.common.util.Assert;
import com.filldream.fastboot.common.util.TransferUtil;
import com.filldream.fastboot.core.cache.CacheMap;
import com.filldream.fastboot.core.cache.CacheObj;
import com.filldream.fastboot.core.cache.CacheUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

/**
 * @author RickSun && iFillDream
 * @version 1.0
 * @data 2020-05-07 18:21
 * @description AccountManager
 */
@Service
public class AccountManager {

    @Resource
    private AccountMapper accountMapper;

    public AccountInfoDTO getById(Long id) {
        AccountManagerDTO accountManagerDTO = selectById(id);
        if(accountManagerDTO == null){
            return null;
        }
        return TransferUtil.changeToObj(accountManagerDTO,AccountInfoDTO.class);
    }

    public AccountManagerDTO selectById(Long id) {
        CacheObj<AccountManagerDTO> cache = CacheUtil.getCache(CacheMap.ACCOUNT_DETAIL, AccountManagerDTO.class, id);
        AccountManagerDTO data = cache.getData();
        if( data != null ){
            return data;
        }

        Account account = accountMapper.selectById(id);
        if(account == null){
            return null;
        }
        AccountManagerDTO accountManagerDTO = TransferUtil.changeToObj(account,AccountManagerDTO.class);
        CacheUtil.cacheObj(cache.getCacheKey(),accountManagerDTO);
        return accountManagerDTO;
    }

    public AccountManagerDTO selectByWxId(String openId,String unionId) {

        LambdaQueryWrapper<Account> sql = new LambdaQueryWrapper<>();
        sql.eq(Account::getDel,0);
        if(StringUtils.isNotBlank(openId)){
            sql.eq( Account::getOepnId,openId );
        }else{
            sql.eq( Account::getUnionId,unionId );
        }
        Account account = accountMapper.selectOne(sql);
        if(account == null){
            return null;
        }
        return TransferUtil.changeToObj(account,AccountManagerDTO.class);
    }

    public boolean modify(Account transfer) {
        AccountManagerDTO accountManagerDTO = selectById(transfer.getId());
        Assert.isEmpty(accountManagerDTO,ErrorCode.BASE_ERR.reMsg("当前用户不存在"));
        int total = accountMapper.updateById(transfer);
        if(total > 0){
            String cacheKey = CacheMap.getKey(CacheMap.ACCOUNT_DETAIL,transfer.getId());
            CacheUtil.delKey(cacheKey);
            return true;
        }
        return false;
    }

    public Account selectByPhone(String telephone) {
        LambdaQueryWrapper<Account> sql = new LambdaQueryWrapper<>();
        sql.eq(Account::getTelephone,telephone);
        sql.eq(Account::getDel,0);
        Account account = accountMapper.selectOne(sql);
        return account;
    }


}
